<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="site-content">
        <div class="brand-info">
          <h2 class="brand-info__text">翻斗幼儿园图书馆管理系统</h2>
        </div>
        <div class="login-main login-container">
          <h3 class="login-title" v-if="isloginvisible">登录</h3>
          <h3 class="login-title" v-if="!isloginvisible">注册</h3>
          <el-form ref="loginForm" :model="loginForm" class="login-form" auto-complete="on" label-position="left" v-if="isloginvisible">

<!--            <div class="title-container">-->
<!--              <h3 class="title">翻斗幼儿园图书馆管理系统</h3>-->
<!--            </div>-->
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input ref="username" v-model="loginForm.username" placeholder="Username" name="username" type="text"
                        tabindex="1" auto-complete="on" />
            </el-form-item>

            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
                        placeholder="Password" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>

            <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                       @click.native.prevent="handleLogin">登录</el-button>

            <div class="tips">
              <span style="margin-right:20px;cursor: pointer;" @click="registerUser">注册</span>
            </div>
          </el-form>
          <el-form ref="form" :model="form" label-width="80px" v-if="!isloginvisible">
            <el-form-item label="学号">
              <el-input v-model="form.userName"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" type="password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码">
              <el-input v-model="form.rePassword" type="password"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="form.phoneNumber"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">立即注册</el-button>
              <a style="margin-left:20px; cursor: pointer; color: #409eff;" @click="loginBtn">去登录</a>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import {register} from "@/api/user";

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      isloginvisible: true,
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      form: {
        userName: '',
        region: '',
        date1: '',
        password: '',
        rePassword: '',
        phoneNumber: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      }
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    registerUser() {
      this.isloginvisible = false
      // this.$router.push({ path: '/register' })
    },
    loginBtn () {
      this.isloginvisible = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true

          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: '/book/book_list' })
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$message({
              message: '用户名或密码有误',
              type: 'warning'
            });
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    onSubmit() {
      console.log('submit!');
      let objSend = {
        userName: this.form.userName,
        password: this.form.password,
        phoneNumber: this.form.phoneNumber
      }
      // 校验密码是否一致
      if (this.form.password !== this.form.rePassword) {
        this.$message({
          type: 'warning',
          message: '两次密码不一致'
        });
        return
      }
      if (!/^1[3-9]\d{9}$/.test(this.form.phoneNumber)) {
        this.$message({
          type: 'warning',
          message: '电话号码格式不正确'
        });
        return
      }
      register(objSend).then(res => {
        this.$message({
          type: 'success',
          message: res.data
        });
        if(res.data == '注册成功'){
          this.isloginvisible = true
          this.loginForm.username = this.form.userName,
          this.loginForm.password = this.form.password
        }
      }).catch(error => {
        reject(error)
      })

    }
  }
}
</script>

<style lang="scss">
.site-wrapper.site-page--login {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(38, 50, 56, .6);
  overflow: hidden;
  &:before {
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    content: "";
    background-image: url(~@/assets/img/beijign.jpg);
    background-size: cover;
  }
  .site-content__wrapper {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0;
    margin: 0;
    overflow-x: hidden;
    overflow-y: auto;
    background-color: transparent;
  }
  .site-content {
    min-height: 100%;
    padding: 30px;
  }
  .brand-info {
    text-align: center;
    margin-top: 12%;
    //margin: 40px 100px 0 90px;
    color: #fff;
  }
  .brand-info__text {
    margin:  0 0 22px 0;
    font-size: 48px;
    font-weight: 400;
    text-transform : uppercase;
  }
  .brand-info__intro {
    margin: 10px 0;
    font-size: 16px;
    line-height: 1.58;
    opacity: .6;
  }
  .login-main {
    position: absolute;
    top: 37%;
    right: 40%;
    padding: 30px 30px 30px;
    width: 409px;
    /* min-height: 100%; */
    background-color: #fff;
  }
  .login-title {
    font-size: 16px;
  }
  .login-captcha {
    overflow: hidden;
    > img {
      width: 100%;
      cursor: pointer;
    }
  }
  .login-btn-submit {
    width: 100%;
    margin-top: 38px;
  }
}

.login-container {
  //.login-form {
  //  position: relative;
  //  width: 520px;
  //  max-width: 100%;
  //  padding: 160px 35px 0;
  //  margin: 0 auto;
  //  overflow: hidden;
  //}

  .tips {
    font-size: 14px;
    color: #409eff;
    margin-bottom: 10px;
    text-align: right;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
}
</style>
